﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ZeroTouchUI
{
    /// <summary>
    /// Interaction logic for AddNewAction.xaml
    /// </summary>
    public partial class NewAddNewAction : Window, INotifyPropertyChanged
    {
        Profile profile;
        public Action oldAction;
        public Action action;

        public event PropertyChangedEventHandler PropertyChanged;
        public event SavedEventHandler Saved;
        public virtual void OnSaved(EventArgs e)
        {
            if (Saved != null)
                Saved(this, e);
        }

        public NewAddNewAction(Profile pro)
        {
            InitializeComponent();
            this.PropertyChanged += new PropertyChangedEventHandler(NewAddNewAction_PropertyChanged);
            DataContext = this;
            profile = pro;
            oldAction = null;
        }

        public NewAddNewAction(Profile pro, string selected)
        {
            InitializeComponent();
            this.PropertyChanged += new PropertyChangedEventHandler(NewAddNewAction_PropertyChanged);
            DataContext = this;
            profile = pro;
            foreach (Action a in profile.getActionList())
            {
                if (a.Name == selected)
                {
                    oldAction = a;
                    break;
                }
            }

            if (oldAction == null)
            {
                return;
            }

            name_textbox.Text = oldAction.Name;
            if (oldAction.Type == "1")
            {
                if (oldAction.CustomData == "1")
                {
                    page1_radiobutton.IsChecked = true;
                }
                else if (oldAction.CustomData == "2")
                {
                    page3_radiobutton.IsChecked = true;
                }

                destination_textbox.Text = oldAction.Info;
            }
            else if (oldAction.Type == "2")
            {
                page2_radiobutton.IsChecked = true;
                targetprogram_textbox.Text = oldAction.Info;
                key2send_textbox.Text = oldAction.CustomData;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void AddOrReplaceAction()
        {
            profile.AddAction(action);
            if (oldAction != null)
            {
                List<Gesture> gestures = new List<Gesture>();
                foreach (Gesture gesture in profile.getDictionary().Keys)
                {
                    gestures.Add(gesture);
                }

                foreach (Gesture gesture in gestures)
                {
                    if (profile.getDictionary()[gesture].Name == oldAction.Name)
                    {
                        profile.getDictionary()[gesture] = action;
                        break;
                    }
                }

                profile.RemoveAction(oldAction);
            }
        }

        private void page1_radiobutton_Checked(object sender, RoutedEventArgs e)
        {
            p1_1_label.Visibility = System.Windows.Visibility.Visible;
            destination_textbox.Visibility = System.Windows.Visibility.Visible;
            example_label.Visibility = System.Windows.Visibility.Visible;
            browseButton2.Visibility = System.Windows.Visibility.Visible;

            p2_1_label.Visibility = System.Windows.Visibility.Hidden;
            p1_2_label.Visibility = System.Windows.Visibility.Hidden;
            example_label2.Visibility = System.Windows.Visibility.Hidden;
            browseButton1.Visibility = System.Windows.Visibility.Hidden;
            targetprogram_textbox.Visibility = System.Windows.Visibility.Hidden;
            p2_2_label.Visibility = System.Windows.Visibility.Hidden;
            key2send_textbox.Visibility = System.Windows.Visibility.Hidden;
            label1.Visibility = System.Windows.Visibility.Hidden;
            label2.Visibility = System.Windows.Visibility.Hidden;
            label3.Visibility = System.Windows.Visibility.Hidden;
            label4.Visibility = System.Windows.Visibility.Hidden;

            this.NotifyPropertyChanged("CanSave");
        }

        private void page2_radiobutton_Checked(object sender, RoutedEventArgs e)
        {
            p1_1_label.Visibility = System.Windows.Visibility.Hidden;
            destination_textbox.Visibility = System.Windows.Visibility.Hidden;
            example_label.Visibility = System.Windows.Visibility.Hidden;
            example_label2.Visibility = System.Windows.Visibility.Hidden;
            browseButton2.Visibility = System.Windows.Visibility.Hidden;
            p1_2_label.Visibility = System.Windows.Visibility.Hidden;

            p2_1_label.Visibility = System.Windows.Visibility.Visible;
            targetprogram_textbox.Visibility = System.Windows.Visibility.Visible;
            browseButton1.Visibility = System.Windows.Visibility.Visible;
            p2_2_label.Visibility = System.Windows.Visibility.Visible;
            key2send_textbox.Visibility = System.Windows.Visibility.Visible;
            label1.Visibility = System.Windows.Visibility.Visible;
            label2.Visibility = System.Windows.Visibility.Visible;
            label3.Visibility = System.Windows.Visibility.Visible;
            label4.Visibility = System.Windows.Visibility.Visible;

            this.NotifyPropertyChanged("CanSave");
        }

        private void page3_radiobutton_Checked(object sender, RoutedEventArgs e)
        {
            p1_2_label.Visibility = System.Windows.Visibility.Visible;
            destination_textbox.Visibility = System.Windows.Visibility.Visible;
            example_label2.Visibility = System.Windows.Visibility.Visible;
            browseButton2.Visibility = System.Windows.Visibility.Visible;

            p2_1_label.Visibility = System.Windows.Visibility.Hidden;
            p1_1_label.Visibility = System.Windows.Visibility.Hidden;
            example_label.Visibility = System.Windows.Visibility.Hidden;
            browseButton1.Visibility = System.Windows.Visibility.Hidden;
            targetprogram_textbox.Visibility = System.Windows.Visibility.Hidden;
            p2_2_label.Visibility = System.Windows.Visibility.Hidden;
            key2send_textbox.Visibility = System.Windows.Visibility.Hidden;
            label1.Visibility = System.Windows.Visibility.Hidden;
            label2.Visibility = System.Windows.Visibility.Hidden;
            label3.Visibility = System.Windows.Visibility.Hidden;
            label4.Visibility = System.Windows.Visibility.Hidden;

            this.NotifyPropertyChanged("CanSave");
        }

        private void addaction_button_Click(object sender, RoutedEventArgs e)
        {
            if (page1_radiobutton.IsChecked == true)
            {
                if (name_textbox.Text != "" && destination_textbox.Text != "")//check for valid input
                {
                    if (profile.getActionList().Any(a => a.Name.ToLower() == name_textbox.Text.ToLower() && oldAction != null && a.Name.ToLower() != oldAction.Name.ToLower()))
                    {
                        NameTaken nt = new NameTaken();
                        nt.ShowDialog();
                    }
                    else
                    {
                        action = new Action();
                        action.Name = name_textbox.Text;
                        action.Info = destination_textbox.Text;
                        action.CustomData = "1";
                        action.Type = "1";
                        AddOrReplaceAction();

                        OnSaved(new EventArgs());
                        this.Close();
                    }
                }
                else
                {
                    // "Please add a webpage or program name in the provided textbox"
                }
            }
            else if (page2_radiobutton.IsChecked == true)
            {
                if (name_textbox.Text != "" && targetprogram_textbox.Text != "" && key2send_textbox.Text != "")
                {
                    if (profile.getActionList().Any(a => a.Name.ToLower() == name_textbox.Text.ToLower() && oldAction != null && a.Name.ToLower() != oldAction.Name.ToLower()))
                    {
                        NameTaken nt = new NameTaken();
                        nt.ShowDialog();
                    }
                    else
                    {
                        action = new Action();
                        action.Name = name_textbox.Text;
                        action.Info = targetprogram_textbox.Text;
                        action.CustomData = key2send_textbox.Text;
                        action.Type = "2";
                        AddOrReplaceAction();

                        OnSaved(new EventArgs());
                        this.Close();
                    }
                }
                else
                {
                    // "Please fill out ALL of the forms provided"
                }
            }
            else if (page3_radiobutton.IsChecked == true)
            {
                if (name_textbox.Text != "" && destination_textbox.Text != "")//check for valid input
                {
                    if (profile.getActionList().Any(a => a.Name.ToLower() == name_textbox.Text.ToLower() && oldAction != null && a.Name.ToLower() != oldAction.Name.ToLower()))
                    {
                        NameTaken nt = new NameTaken();
                        nt.ShowDialog();
                    }
                    else
                    {
                        action = new Action();
                        action.Name = name_textbox.Text;
                        action.Info = destination_textbox.Text;
                        action.CustomData = "2";
                        action.Type = "1";
                        AddOrReplaceAction();

                        OnSaved(new EventArgs());
                        this.Close();
                    }
                }
                else
                {
                    // "Please add a webpage or program name in the provided textbox"
                }
            }
            else
            {
                // "Please select a type of event to add
            }
        }

        private void cancel_button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            open.DefaultExt = ".exe";

            if (open.ShowDialog() == true && System.IO.Path.GetExtension(open.FileName) == ".exe")
            {
                if (((Button)sender).Name == "browseButton1")
                {
                    targetprogram_textbox.Text = System.IO.Path.GetFileNameWithoutExtension(open.FileName);
                }
                else if (((Button)sender).Name == "browseButton2")
                {
                    destination_textbox.Text = System.IO.Path.GetFileNameWithoutExtension(open.FileName);
                }
            }
            else
            {
                if (open.FileName != String.Empty)
                {
                    System.Windows.MessageBox.Show("Please select an executable application file.\n\n Example: \"notepad.exe\"",
                                                   "Incorrect File Type", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }

        public bool CanSave
        {
            get
            {
                return ((page1_radiobutton.IsChecked == true && !String.IsNullOrWhiteSpace(name_textbox.Text) && !String.IsNullOrWhiteSpace(destination_textbox.Text)) ||
                        (page2_radiobutton.IsChecked == true && !String.IsNullOrWhiteSpace(name_textbox.Text) && !String.IsNullOrWhiteSpace(targetprogram_textbox.Text) && !String.IsNullOrWhiteSpace(key2send_textbox.Text)) ||
                        (page3_radiobutton.IsChecked == true && !String.IsNullOrWhiteSpace(name_textbox.Text) && !String.IsNullOrWhiteSpace(destination_textbox.Text)));
            }
        }

        private void NewAddNewAction_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new ThreadStart(() => addaction_button.IsEnabled = CanSave));
        }

        protected void NotifyPropertyChanged(string info)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private void textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.NotifyPropertyChanged("CanSave");
        }
    }
}
